package com.swsc.solarverse.common.mybatis;

import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * @Author chencl
 * @Date 2022/1/18 14:43
 * @Version 1.0
 * @Description TransactionCommit
 */
public class TransactionCommit {
    public static void ifAfter(Runnable runnable) {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            // TransactionSynchronizationAdapter 类过时, 使用TransactionSynchronization
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                @Override
                public void afterCommit() {
                    runnable.run();
                }
            });
        } else {
            runnable.run();
        }
    }
}
